package com.canbot.u05.adapter.sales;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;

import com.canbot.u05.R;

import java.util.ArrayList;
import java.util.List;

public class ItemSelectMapSalesAdapter<T> extends BaseAdapter {

        private List<T> objects = new ArrayList<T>();

        private Context context;

        private LayoutInflater layoutInflater;

        private ViewHolder holder;

        public ItemSelectMapSalesAdapter(Context context, List<T> objects) {
                this.context = context;
                this.layoutInflater = LayoutInflater.from(context);
                this.objects = objects;
        }

        @Override
        public int getCount() {
                return objects.size();
        }

        @Override
        public T getItem(int position) {
                return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                        convertView = layoutInflater.inflate(R.layout.item_select_map_sales, null);
                        holder = new ViewHolder(convertView);
                        convertView.setTag(holder);
                }
                initializeViews(getItem(position), (ViewHolder) convertView.getTag());
                return convertView;
        }

        private void initializeViews(T object, ViewHolder holder) {
                //TODO implement 加载图片
        }

        protected class ViewHolder {

                private ImageView ivMap;

                private CheckBox cbSelect;

                public ViewHolder(View view) {
                        ivMap = (ImageView) view.findViewById(R.id.iv_map);
                        cbSelect = (CheckBox) view.findViewById(R.id.cb_select);
                }
        }

        /**
         * 更新控件显示
         *
         * @param position
         * @param gridView
         */
        public void updataView(int position, GridView gridView) {
                int visibleFirstPosi = gridView.getFirstVisiblePosition();
                int visibleLastPosi = gridView.getLastVisiblePosition();
                if (position >= visibleFirstPosi && position <= visibleLastPosi) {

                        View view = gridView.getChildAt(position - visibleFirstPosi);
                        ItemSelectMapSalesAdapter.ViewHolder holder = (ItemSelectMapSalesAdapter.ViewHolder) view.getTag();
                        holder.cbSelect.setChecked(true);
                        //holder.checkBox.setChecked(data.get(position).isChecked());

                }
                else {

                }


        }
}
